//
//  MeSmallCell.m
//  LingQian
//
//  Created by wtjr on 2018/1/22.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "MeTableViewCell.h"
#import "MeSmallCell.h"

@interface MeTableViewCell()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MeTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    static NSString *cellId = @"MeTableViewCell";
    MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[MeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - init

- (void)initSubView {
    self.backgroundColor = kCOLOR_clear;
    [self addSubview:self.mainView];
}

- (void)autoLayoutSubviews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(10, 20, 10, 20));
    }];
}

#pragma mark - Method


#pragma mark - Delegate

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _indexPath.section == 1 ? self.viewModel.dateSource1.count : self.viewModel.dateSource2.count;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeSmallCell *cell = [MeSmallCell cellWithTableView:tableView indexPath:indexPath];
    cell.indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:_indexPath.section];
    cell.viewModel = _viewModel;
    cell.cellLineType = indexPath.row == 2 ? kCellLineTypeNone : kCellLineTypeNormal;
    [cell setMarginLeft:20 right:-20];
    return cell;
}

#pragma mark - Setter

- (void)setViewModel:(MeViewModel *)viewModel {
    _viewModel = viewModel;
    [self.tableView reloadData];
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
}

#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        _mainView.layer.cornerRadius = kUI_LayerCorner;
        [_mainView setViewShadow:kCOLOR_shadowLight offset:CGSizeMake(0, 0) radius:kUI_ShawRadius/2];
        
        [_mainView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@0);
        }];
    }
    return _mainView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_white;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = kUI_CellHeight;
        _tableView.bounces = YES;
        _tableView.backgroundColor = kCOLOR_white;
        _tableView.layer.cornerRadius = kUI_LayerCorner;
    }
    return _tableView;
}

@end
